/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/02/18
* File: enn_vector4.h
**/

#pragma once

#include "enn_vector3.h"

namespace enn
{
/// Four-dimensional vector.
template <typename T> class Vector4
{
public:
	/// Construct undefined.
	Vector4()
	{
	}

	/// Copy-construct from another vector.
	Vector4(const Vector4<T>& vector) :
	x_(vector.x_),
		y_(vector.y_),
		z_(vector.z_),
		w_(vector.w_)
	{
	}

	/// Construct from a 3-dimensional vector and the W coordinate.
	Vector4(const Vector3<T>& vector, T w) :
	x_(vector.x_),
		y_(vector.y_),
		z_(vector.z_),
		w_(w)
	{
	}

	/// Construct from coordinates.
	Vector4(T x, T y, T z, T w) :
	x_(x),
		y_(y),
		z_(z),
		w_(w)
	{
	}

	/// Construct from a T array.
	Vector4(const T* data) :
	x_(data[0]),
		y_(data[1]),
		z_(data[2]),
		w_(data[3])
	{
	}

	/// Assign from another vector.
	Vector4& operator = (const Vector4<T>& rhs)
	{
		x_ = rhs.x_;
		y_ = rhs.y_;
		z_ = rhs.z_;
		w_ = rhs.w_;
		return *this;
	}

	/// Test for equality with another vector without epsilon.
	bool operator == (const Vector4<T>& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_ && w_ == rhs.w_; }
	/// Test for inequality with another vector without epsilon.
	bool operator != (const Vector4<T>& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_; }
	/// Add a vector.
	Vector4<T> operator + (const Vector4<T>& rhs) const { return Vector4<T>(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_, w_ + rhs.w_); }
	/// Return negation.
	Vector4<T> operator - () const { return Vector4<T>(-x_, -y_, -z_, -w_); }
	/// Subtract a vector.
	Vector4<T> operator - (const Vector4<T>& rhs) const { return Vector4<T>(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_, w_ - rhs.w_); }
	/// Multiply with a scalar.
	Vector4<T> operator * (T rhs) const { return Vector4<T>(x_ * rhs, y_ * rhs, z_ * rhs, w_ * rhs); }
	/// Multiply with a vector.
	Vector4<T> operator * (const Vector4<T>& rhs) const { return Vector4<T>(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_, w_ * rhs.w_); }
	/// Divide by a scalar.
	Vector4<T> operator / (T rhs) const { return Vector4<T>(x_ / rhs, y_ / rhs, z_ / rhs, w_ / rhs); }
	/// Divide by a vector.
	Vector4<T> operator / (const Vector4<T>& rhs) const { return Vector4<T>(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_, w_ / rhs.w_); }

	/// Add-assign a vector.
	Vector4<T>& operator += (const Vector4<T>& rhs)
	{
		x_ += rhs.x_;
		y_ += rhs.y_;
		z_ += rhs.z_;
		w_ += rhs.w_;
		return *this;
	}

	/// Subtract-assign a vector.
	Vector4<T>& operator -= (const Vector4<T>& rhs)
	{
		x_ -= rhs.x_;
		y_ -= rhs.y_;
		z_ -= rhs.z_;
		w_ -= rhs.w_;
		return *this;
	}

	/// Multiply-assign a scalar.
	Vector4<T>& operator *= (T rhs)
	{
		x_ *= rhs;
		y_ *= rhs;
		z_ *= rhs;
		w_ *= rhs;
		return *this;
	}

	/// Multiply-assign a vector.
	Vector4<T>& operator *= (const Vector4<T>& rhs)
	{
		x_ *= rhs.x_;
		y_ *= rhs.y_;
		z_ *= rhs.z_;
		w_ *= rhs.w_;
		return *this;
	}

	/// Divide-assign a scalar.
	Vector4<T>& operator /= (T rhs)
	{
		x_ /= rhs;
		y_ /= rhs;
		z_ /= rhs;
		w_ /= rhs;
		return *this;
	}

	/// Divide-assign a vector.
	Vector4<T>& operator /= (const Vector4<T>& rhs)
	{
		x_ /= rhs.x_;
		y_ /= rhs.y_;
		z_ /= rhs.z_;
		w_ /= rhs.w_;
		return *this;
	}

	/// Calculate dot product.
	T DotProduct(const Vector4<T>& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_ + w_ * rhs.w_; }
	/// Calculate absolute dot product.
	T AbsDotProduct(const Vector4<T>& rhs) const { return enn::Abs(x_ * rhs.x_) + enn::Abs(y_ * rhs.y_) + enn::Abs(z_ * rhs.z_) + enn::Abs(w_ * rhs.w_); }
	/// Return absolute vector.
	Vector4<T> Abs() const { return Vector4<T>(enn::Abs(x_), enn::Abs(y_), enn::Abs(z_), enn::Abs(w_)); }
	/// Linear interpolation with another vector.
	Vector4<T> Lerp(const Vector4<T>& rhs, T t) const { return *this * (1.0f - t) + rhs * t; }
	/// Test for equality with another vector with epsilon.
	bool Equals(const Vector4<T>& rhs) const { return enn::Equals(x_, rhs.x_) && enn::Equals(y_, rhs.y_) && enn::Equals(z_, rhs.z_) && enn::Equals(w_, rhs.w_); }

	/// Return T data.
	const T* Data() const { return &x_; }

	/// X coordinate.
	T x_;
	/// Y coordinate.
	T y_;
	/// Z coordinate.
	T z_;
	/// W coordinate.
	T w_;

	/// Zero vector.
	static const Vector4<T> ZERO;
	/// (1,1,1) vector.
	static const Vector4<T> ONE;
};

/// Multiply Vector4 with a scalar.
template <typename T>
inline Vector4<T> operator * (T lhs, const Vector4<T>& rhs) { return rhs * lhs; }

typedef Vector4<float> vec4f;
typedef Vector4<double> vec4d;
typedef Vector4<int> vec4i;
typedef Vector4<unsigned int> vec4ui;

}